package cn.edu.gzgs.controller;

import cn.edu.gzgs.dto.DeptDTO;
import cn.edu.gzgs.dto.DeptPageQueryDTO;
import cn.edu.gzgs.entity.Dept;
import cn.edu.gzgs.service.DeptService;
import cn.edu.gzgs.vo.ApiResponse;
import cn.edu.gzgs.vo.DeptUserVO;
import cn.edu.gzgs.vo.DeptVO;
import cn.edu.gzgs.common.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理API接口
 *
 * @author Zyf
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/api/dept")
@Tag(name = "部门管理", description = "提供部门的增删改查等接口")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 新增部门
     *
     * @param deptDTO 部门数据
     * @return Result
     */
    @PostMapping
    @Operation(summary = "新增部门")
    @PreAuthorize("hasAuthority('admin')")
    public ResponseEntity<ApiResponse<Void>> save(@Valid @RequestBody DeptDTO deptDTO) {
        log.info("新增部门: {}", deptDTO);
        deptService.saveDept(deptDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(new ApiResponse<>(0, "部门新增成功", null));
    }

    /**
     * 根据ID删除部门
     *
     * @param id 部门ID
     * @return Result
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门")
    @PreAuthorize("hasAuthority('admin') and @permissionService.checkDeptPermission(#id)")
    @Parameter(name = "id", description = "部门ID", required = true)
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        log.info("根据ID删除部门: {}", id);
        deptService.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 修改部门信息
     *
     * @param deptDTO 部门数据
     * @return Result
     */
    @PutMapping
    @Operation(summary = "修改部门")
    @PreAuthorize("hasAuthority('admin') and @permissionService.checkDeptPermission(#deptDTO.id)")
    public ResponseEntity<ApiResponse<Void>> update(@Valid @RequestBody DeptDTO deptDTO) {
        log.info("修改部门信息: {}", deptDTO);
        deptService.updateDept(deptDTO);
        return ResponseEntity.ok(new ApiResponse<>(0, "部门修改成功", null));
    }

    /**
     * 根据ID查询部门
     *
     * @param id 部门ID
     * @return Result<Dept>
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询部门")
    @Parameter(name = "id", description = "部门ID", required = true)
    public ResponseEntity<ApiResponse<Dept>> getById(@PathVariable Long id) {
        log.info("根据ID查询部门: {}", id);
        Dept dept = deptService.getById(id);
        return ResponseEntity.ok(new ApiResponse<>(0, "查询成功", dept));
    }

    /**
     * 分页查询部门
     *
     * @param deptPageQueryDTO 查询条件
     * @return Result<PageResult>
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询部门")
    @PreAuthorize("hasAuthority('admin')")
    public ResponseEntity<ApiResponse<PageResult<DeptVO>>> pageQuery(DeptPageQueryDTO deptPageQueryDTO) {
        log.info("分页查询部门: {}", deptPageQueryDTO);
        PageResult<DeptVO> pageResult = deptService.pageQuery(deptPageQueryDTO);
        return ResponseEntity.ok(new ApiResponse<>(0, "查询成功", pageResult));
    }

    /**
     * 查询所有部门
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有部门列表")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<ApiResponse<List<DeptVO>>> listAll() {
        log.info("查询所有部门");
        List<DeptVO> deptList = deptService.listAll();
        return ResponseEntity.ok(new ApiResponse<>(0, "查询成功", deptList));
    }

    /**
     * 根据部门ID查询其下的所有用户
     */
    @GetMapping("/{id}/users")
    @Operation(summary = "根据部门ID查询其下的所有用户（精简信息）")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<ApiResponse<List<DeptUserVO>>> getDeptUsers(@PathVariable Long id) {
        log.info("根据部门ID查询用户: {}", id);
        List<DeptUserVO> deptUsers = deptService.getDeptUsersByDeptId(id);
        return ResponseEntity.ok(new ApiResponse<>(0, "查询成功", deptUsers));
    }
} 